$(function () {
    $.ajax({
        type:"POST",
        url: ApiUrl + "/index.php?act=article&op=index",
        dataType:"json",
        success:function (res) {
            //console.log(res);
            if(res['status']==200){
                if(!isObjEmpty(res['data'])){
                    FillGoodsDatas(res['data']);
                }else{
                    // 数据为空的处理
                    $('.nctouch-norecord search').show();
                }
            }
        }
    })

})

/**
 * 判断json数据是否为空
 * @param data
 */
function isObjEmpty(data) {
    for(k in data){
         return 0
    }
    return 1
}

/**
 * 根据地区id请求数据
 * @param aid
 */
function getArcticle(aid) {
    var inputHidden = document.getElementsByClassName('address');
    var flag = '';
    for (var i=0;i<inputHidden.length;i++){
        var name = inputHidden[i].name;
        var val   = inputHidden[i].value;
        if(aid==val){
            flag = name;
        }
    }
    var data= "{"+flag+":"+aid+"}";
    data = eval('(' + data + ')');
    $.ajax({
        type:"POST",
        url: ApiUrl + "/index.php?act=article&op=index",
        data:data,
        dataType:"json",
        success:function (res) {
            //console.log(res);
            if(res['status']==200){
                if(!isObjEmpty(res['data'])){
                    FillGoodsDatas(res['data']);
                }else{
                   return "没有数据上传！"
                }
            }

        }
    })
}
//下拉刷新
var page = 1;
var key = getCookie('key');
var isLock = false;
$(window).scroll(function () {
    var windowheight = $(window).height();//窗口高度
    var scrolltop = $('body').scrollTop();//滚动条高度
    var eh = $(document).height();//文本高度
    if (parseInt(windowheight + scrolltop)+2 >= eh && isLock == false) {
        isLock = true;
        page++;
        $.ajax({
            type:"GET",
            url: ApiUrl + "/index.php?act=article&op=index&page="+page,
            data:{key:key},
            dataType:"json",
            success:function(data){
                var comment = data['data'];
                if (data['status']==200){
                    if (comment==undefined){
                        $(".nodata").show();
                    }else{
                        var tempHtml = "";
                        for (var i=0;i<data.length;i++){
                            tempHtml += '<a style="color:#666;" href="article_detail.html?article_id='+data[i]['article_id']+'"><li>' + '<img style="width: 70px;height: 70px;" class="img1" src='+data[i]['ImageUrl']+' alt=""/>' +
                                '<div class="center">' +
                                '<p>'+data[i]['article_title']+'</p>' +
                                '<p>文章短标题:<span class="num">'+data[i]['article_title_short']+'</span></p>' +
                                '<p>上线时间:<span>'+data[i]['date']+'</span></p>' +
                                '<p>' +
                                '<span class="look">'+data[i]['article_click']+'</span><span  class="good" onclick="dorp(this,'+data[i]['article_id']+')" >'+data[i]['article_comment_count']+'</span></p>' +
                                '</div></li></a>';
                        }
                        $('.list').html($('.list').html()+tempHtml);

                    }

                }
                isLock = false;
            }
        })
        //alert('123');
    }
})

function isEmptyJson(json) {
    for(k in json){
        return 0
    }
    return 1;
}

/**
 * 填充商品数据
 * @param data 数据
 * @constructor
 */
function FillGoodsDatas(data){
     var tempHtml = "";
     for (var i=0;i<data.length;i++){
            tempHtml += '<a style="color:#666;" href="article_detail.html?article_id='+data[i]['article_id']+'"><li>' + '<img style="width: 70px;height: 70px;" class="img1" src='+data[i]['ImageUrl']+' alt=""/>' +
                '<div class="center">' +
                '<p>'+data[i]['article_title']+'</p>' +
                '<p>文章短标题:<span class="num">'+data[i]['article_title_short']+'</span></p>' +
                '<p>上线时间:<span>'+data[i]['date']+'</span></p>' +
                '<p>' +
                '<span class="look">'+data[i]['article_click']+'</span><span  class="good" onclick="dorp(this,'+data[i]['article_id']+')" >'+data[i]['article_comment_count']+'</span></p>' +
                '</div></li></a>';
     }
    //console.log(tempHtml);
    //   console.log(data);
     $('.list').html(tempHtml);
}


